package fr.umlv.escapeir.structure;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import fr.umlv.escapeir.gesture.Direction;

public class LinearMove  implements Behavior {
	
	private static final int TIME = 60;
	private final Body body;
	private final float x;
	private final float y;
	
	private int tick;
	
	private LinearMove(Body body, float x, float y) {
		this.body = body;
		this.x = x;
		this.y = y;
	}

	@Override
	public void apply() {
		if (tick > TIME) return;
		if (tick == TIME) body.setLinearVelocity(new Vec2(0,0));
		if (tick == 0) body.applyLinearImpulse(new Vec2(x,y), body.getWorldCenter());
		tick++;
	}
	
	public static LinearMove createOnBody(Body b, Direction dir, int value) {
		int x = 0;
		int y = 0;
		switch(dir) {
			case Down:
				x = 0;
				y = -value;
				break;
			case DownLeft:
				x = -value;
				y = -value;
				break;
			case DownRight:
				x = value;
				y = -value;
				break;
			case Left:
				x = -value;
				y = 0;
				break;
			case Right:
				x = value;
				y = 0;
				break;
			case Top:
				x = 0;
				y = value;
				break;
			case TopLeft:
				x = -value;
				y = value;
				break;
			case TopRight:
				x = value;
				y = value;
				break;
			default:
				break;
		}
		LinearMove lm = new LinearMove(b, x, y);
		return lm;
	}

}
